/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.ui;

import utsc.apps.utschedule.ui.ColorPickerDialog.OnColorChangedListener;
import android.app.Dialog;
import android.os.Bundle;

import com.actionbarsherlock.app.SherlockDialogFragment;

/**
 * A dialog fragment to allow user to select a color from a basic color wheel. 
 *
 */
public class MyColorPickerDialogFragment extends SherlockDialogFragment implements OnColorChangedListener{
	public static final String TAG="colorPickerDialogFragment";
	private static final String INITIAL_COLOR_KEY="initialColor";
	int initialColor;
	
	/**
	 * A callback interface where being called once the user has selected a
	 * color.
	 * 
	 */
	public interface ChangeColorCellViewListener{
		void onFinishColorPickerDialog(int color);
	}
	 
	/**
	 * Required empty constructor.
	 */
	public MyColorPickerDialogFragment() {
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRetainInstance(true);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		Bundle b=getArguments();
		initialColor=b.getInt(INITIAL_COLOR_KEY);
		//if (viewToUpdateColor!=null) {
			ColorPickerDialog dialog=new ColorPickerDialog(getSherlockActivity(), this, initialColor);
			//AmbilWarnaDialog dialog=new AmbilWarnaDialog(getSherlockActivity(), initialColor, viewToUpdateColor);
			return dialog;
	}
	
	/**
	 * Recommended method to create a new instance.
	 * @return
	 */
	public static MyColorPickerDialogFragment newInstance(){
		return new MyColorPickerDialogFragment();
	}
	
	/**
	 * Recommended method to create a new instance with a pre-defined color.
	 * @param initialColor
	 * @return
	 */
	public static MyColorPickerDialogFragment newInstance(int initialColor) {
		Bundle b=new Bundle();
		b.putInt(INITIAL_COLOR_KEY, initialColor);
		
		MyColorPickerDialogFragment fragment=new MyColorPickerDialogFragment();
		fragment.setArguments(b);
		return fragment;
	}
	
	@Override
	public void onDestroyView() {
		if (getDialog() != null && getRetainInstance())
		    getDialog().setOnDismissListener(null);
		super.onDestroyView();
	}
	
	@Override
	public void colorChanged(int color) {
		ChangeColorCellViewListener activity=(ChangeColorCellViewListener) getSherlockActivity();
		activity.onFinishColorPickerDialog(color);
	}
	
	
}
